package com.example.statemachine.service.impl;

import com.example.statemachine.entity.Credential;
import com.example.statemachine.service.BaseService;
import com.example.statemachine.util.BaseMapper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jianwei.wang
 * @version v1.0
 * @date 2019/5/13 11:24
 **/
public class BaseServiceImpl<T> implements BaseService<T> {

    @Resource
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> selectAll() {
        return baseMapper.selectAll();
    }

    @Override
    public List<T> select(T record) {
        return baseMapper.select(record);
    }

    @Override
    public int selectCount(T record) {
        return baseMapper.selectCount(record);
    }

    @Override
    public T selectByPrimaryKey(Object key) {
        return baseMapper.selectByPrimaryKey(key);
    }

    @Override
    public int insert(T record) {
        return baseMapper.insert(record);
    }

    @Override
    public int insertSelective(T record) {
        return baseMapper.insertSelective(record);
    }

    @Override
    public int delete(T key) {
        return baseMapper.delete(key);
    }

    @Override
    public int deleteByPrimaryKey(Object key) {
        return baseMapper.deleteByPrimaryKey(key);
    }

    @Override
    public int updateByPrimaryKey(T record) {
        return baseMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return baseMapper.updateByPrimaryKeySelective(record);
    }

    protected int updateById(Credential credential) {
        Example example = Example.builder(Credential.class).andWhere(Sqls.custom().andEqualTo("id", credential.getId())).build();
        return baseMapper.updateByExampleSelective((T) credential,example);
    }
}
